<template>
  <div class="mod-oss">
    <el-form :inline="true" :model="fileDto">
      <el-form-item>
        <el-select v-model="fileDto.type" placeholder="文件类型" clearable>
         <el-option value="1" label="图片">图片</el-option>
         <el-option value="2" label="文档">文档</el-option>
         <el-option value="3" label="表格">表格</el-option>
         <el-option value="4" label="视频">视频</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="fileDto.remark" placeholder="备注名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetFind()" type="info">重置</el-button>
        <el-button type = "success" @click="getDataList()">查询</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button type="primary" @click="uploadHandle()">上传文件</el-button> 
        <el-button type="primary" @click="downloadHandle()" :disabled="dataListSelections.length <= 0">下载文件</el-button>
        <el-button type="primary" @click="configHandle()">云存储配置</el-button> 
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="fileName" header-align="center" align="center" label="文件名称" width="300"></el-table-column>
      <el-table-column prop="filePath" header-align="center" align="center" label="文件路径">
        <template slot-scope="scope">
          <a :href="scope.row.filePath" target="_blank" class="buttonText">{{scope.row.filePath}}</a>
        </template>
      </el-table-column>
      <el-table-column prop="type" header-align="center" align="center" label="类型" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 1" size="small" type="success">图片</el-tag>
          <el-tag v-else-if="scope.row.type === 2" size="small" type="success">文档</el-tag>
          <el-tag v-else-if="scope.row.type === 3" size="small" type="success">表格</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注名" width="120"></el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间" width="200"></el-table-column>     
      <el-table-column fixed="right" header-align="center" align="center" label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button type="success" size="mini" @click="downloadHandle(scope.row.id)">下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle"
      :current-page="fileDto.pageNum" :page-sizes="[10, 20, 50, 100]" :page-size="fileDto.pageSize"
      :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 云存储配置 -->
    <config v-if="configVisible" ref="config"></config>
    <!-- 弹窗, 上传文件 -->
    <upload v-if="uploadVisible" ref="upload" @refreshDataList="getDataList"></upload>
  </div>
</template>

<script>
  import Config from './oss-config'
  import Upload from './oss-upload'
  export default {
    data () {
      return {
        fileDto: {
          type: '',
          remark: '',
          pageNum: 1,
          pageSize: 10
        },
        fileDeleteDto: {
          ids: [],
          fileNames: []
        },
        dataForm: {},
        dataList: [],
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        configVisible: false,
        uploadVisible: false
      }
    },
    components: {
      Config,
      Upload
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/admin-interfaces/file/pageFiles'),
          method: 'post',
          data: this.fileDto
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.dataList = data.data.records
            this.totalPage = data.data.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数量变化
      sizeChangeHandle (val) {
        this.fileDto.pageSize = val
        this.fileDto.pageNum = 1
        this.getDataList()
      },
      // 跳转指定页数
      currentChangeHandle (val) {
        this.fileDto.pageNum = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 云存储配置
      configHandle () {
        this.configVisible = true
        this.$nextTick(() => {
          this.$refs.config.init()
        })
      },
      // 上传文件
      uploadHandle () {
        this.uploadVisible = true
        this.$nextTick(() => {
          this.$refs.upload.init()
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        const foundItem = this.dataList.find(item => item.id === id)
        var fileNames = id ? [foundItem.fileName] : this.dataListSelections.map(item => {
          return item.fileName
        })
        this.fileDeleteDto.ids = ids
        this.fileDeleteDto.fileNames = fileNames
        this.$confirm(`是否对【${fileNames.join('、')}】进行${id ? '删除' : '批量删除'}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/admin-interfaces/file/deleteFile'),
            method: 'post',
            data: this.fileDeleteDto
          }).then(({data}) => {
            if (data.code === 200 && data.data === true) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message({
                showClose: true,
                message: data.message,
                type: 'warning',
                center: true
              })
            }
          })
        }).catch(() => {})
      },
      // 下载
      downloadHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        const foundItem = this.dataList.find(item => item.id === id)
        var fileNames = id ? [foundItem.fileName] : this.dataListSelections.map(item => {
          return item.fileName
        })
        this.fileDeleteDto.ids = ids
        this.fileDeleteDto.fileNames = fileNames
        this.$confirm(`是否下载以下文件：${fileNames.join('、')}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/admin-interfaces/file/download'),
            method: 'post',
            data: this.fileDeleteDto
          }).then(({data}) => {
            if (data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message({
                showClose: true,
                message: data.message,
                type: 'warning',
                center: true
              })
            }
          })
        }).catch(() => {})
      },
      // 重置查询
      resetFind () {
        this.fileDto.type = ''
        this.fileDto.remark = ''
        this.getDataList()
      }
    }
  }
</script>
